﻿#include "XyNet/Global.h"
#include "XyNet/XyNet.h"
#include "XyNetTest/Chat/ChatClient.h"
#include "XyNetTest/Chat/ChatServer.h"
#include "XyNetTest/Test.h"

namespace XyNetTest
{
	void testSingleServer(int clientNumber)
	{
		boost::format format("测试单服务器（客户端数量: %s）: ");
		format % clientNumber;
		XyNet::Logger::getDefault()->log(format.str());
		auto testServerManager = ChatServerManager::getInstance();
		auto testClientManager = TestClientManager::getInstance();
		testClientManager->test(testServerManager->initServer(), clientNumber);
	}

	void testChat()
	{
		XyNet::ProcessTimer timer;

		//vector<int> clientNumberList = { 3 };	// 1s
		//vector<int> clientNumberList = { 7 };
		//vector<int> clientNumberList = { 50 };
		//vector<int> clientNumberList = { 100 };
		//vector<int> clientNumberList = { 200 };
		//vector<int> clientNumberList = { 250 };
		//vector<int> clientNumberList = { 500 };	// 16s?
		//vector<int> clientNumberList = { 1000 };	// 86s / 2
		//vector<int> clientNumberList = { 2000 };	// 247s?
		//vector<int> clientNumberList = { 3000 };	// 1344s / 2
		//vector<int> clientNumberList = { 5000 };	// 1400.547s?
#ifdef _DEBUG
		vector<int> clientNumberList = { 3, 5, 7, 9, 16, 25, 50, 100, 200 };
		clientNumberList = { 50 };
#else
		vector<int> clientNumberList = { 3, 5, 7, 9, 16, 25, 50, 100, 200, 400 };
		clientNumberList = { 3000 };
#endif
		clientNumberList = { 3 };

		cout << "Test Client Numbers: ";
		for (int clientNumber : clientNumberList)
			cout << clientNumber << " ";
		cout << endl;

		auto logger = XyNet::Logger::getDefault();

		for (auto clientNumber : clientNumberList)
		{
			if (clientNumberList.size() == 1 && clientNumberList[0] == 3)
				logger->setWriteToConsole(true);
			else
				logger->setWriteToConsole(false);

			testSingleServer(clientNumber);

			//logger->setWriteToConsole(false);
			logger->log("关闭客户端: ");
			TestClientManager::getInstance()->destroy();
			logger->log("关闭服务端: ");
			ChatServerManager::getInstance()->destroy();
		}

		//TestClientManager::destroy();

		timer.print();
		XyNet::System::popupMessageBox(lexical_cast<string>(timer.elapsed()) + "秒");
	}
}